package com.xiawu;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ServerWX {
    public static void main(String[] args) throws  Exception {
        // 1，创建服务器对象
        ServerSocket server = new ServerSocket(9000);
        System.out.println("===========服务器端================");
        // 2,接收客户端的链接
        Socket cli = server.accept();
        // 3,获取对客户端的 字符的输入输出流
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(cli.getOutputStream()));
        BufferedReader br = new BufferedReader(new InputStreamReader(cli.getInputStream()));
        // 4,先接收客户端的语句，然后给出回复
        while(true){
            String s = br.readLine();
            System.out.println("客户端："+ s);
            if(s.equals("byebye")){  //终止聊天
                break;
            }
            Scanner sc = new Scanner(System.in);
            System.out.println("请回复");
            String s1 = sc.next();
            bw.write(s1);
            bw.newLine();
            bw.flush();
        }


        // 5,释放资源
        bw.close();
        br.close();
        cli.close();
        server.close();

    }
}
